//
//  HelloWorldLayer.m
//  game1
//
//  Created by iD Student on 6/26/13.
//  Copyright iD Student 2013. All rights reserved.
//


// Import the interfaces
#import "HelloWorldLayer.h"
 
// Needed to obtain the Navigation Controller
#import "AppDelegate.h"

#pragma mark - HelloWorldLayer

#define playerleft (player.position.x - player.boundingBox.size.width/2)
#define playerright (player.position.x + player.boundingBox.size.width/2)
#define playertop (player.position.y + player.boundingBox.size.height/2)
#define playerbot (player.position.y - player.boundingBox.size.height/2)
#define playercentx (player.position.x)
#define playercenty (player.position.y)

#define oneleft (ground[1].position.x - ground[1].boundingBox.size.width/2)
#define oneright (ground[1].position.x + ground[1].boundingBox.size.width/2)
#define onetop (ground[1].position.y + ground[1].boundingBox.size.height/2)
#define onebot (ground[1].position.y - ground[1].boundingBox.size.height/2)

#define twoleft (ground[2].position.x - ground[2].boundingBox.size.width/2)
#define tworight (ground[2].position.x + ground[2].boundingBox.size.width/2)
#define twotop (ground[2].position.y + ground[2].boundingBox.size.height/2)
#define twobot (ground[2].position.y - ground[2].boundingBox.size.height/2)


// HelloWorldLayer implementation
@implementation HelloWorldLayer
CCSprite *player;
CCSprite *ground[5];

// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
    // 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	HelloWorldLayer *layer = [HelloWorldLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}



float xvelocity = -2;
float yvelocity = -1;

int score = 0;


CCLabelTTF *label;


// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super's" return value
    if( (self=[super init]) ) {
        CGSize winSize = [CCDirector sharedDirector].winSize;
        
        ground[1] = [CCSprite spriteWithFile:@"rectangle1.jpg"];
        ground[2] = [CCSprite spriteWithFile:@"rectangle1.jpg"];
        
		player = [CCSprite spriteWithFile:@"playerSprite1.png"];
        
        label = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%i", score] fontName:@"courier" fontSize:100];
        
        [player setPosition:ccp(winSize.width * 0.4, winSize.height * 0.6)];
        [ground[1] setPosition:ccp(winSize.width * 0.3, winSize.height * 0.5)];
        [ground[2] setPosition:ccp(winSize.width * 0.3, winSize.height * 0.5)];
        
        [self addChild:player];
        [self addChild:ground[1]];
        [self addChild:ground[2]];
        [self addChild:label];
        [self schedule:@selector(update:)]; // schedules the update function to run every frame
        [self schedule:@selector(collisionDetection:)];   
        [self setTouchEnabled:TRUE];
        
	}
	return self;
}

-(void)update:(ccTime)delta{
    CGSize winSize = [CCDirector sharedDirector].winSize;
    [player setPosition:ccp(player.position.x + xvelocity, player.position.y + yvelocity)];
    [label setString:([NSString stringWithFormat:@"%i", score])];
    [label setPosition:ccp(460,280)];
    if(label.position.x + label.boundingBox.size.width/2 > 480){
        [label setPosition:ccp(winSize.width - label.boundingBox.size.width/2,winSize.height - label.boundingBox.size.height/2)];
    }
    if(score >= 1){
        xvelocity = ((xvelocity / abs(xvelocity) * 4));
        yvelocity = ((yvelocity / abs(yvelocity) * 2));
        if(score >= 5){
            xvelocity = ((xvelocity / abs(xvelocity) * 5));
            yvelocity = ((yvelocity / abs(yvelocity) * 2.5));
            if(score >= 10){
                xvelocity = ((xvelocity / abs(xvelocity) * 6));
                yvelocity = ((yvelocity / abs(yvelocity) * 3));
                if(score >= 20){
                    xvelocity = ((xvelocity / abs(xvelocity) * 8));
                    yvelocity = ((yvelocity / abs(yvelocity) * 4));
                    if(score >= 30){
                        xvelocity = ((xvelocity / abs(xvelocity) * 10));
                        yvelocity = ((yvelocity / abs(yvelocity) * 5));
                        if(score >= 40){
                            xvelocity = ((xvelocity / abs(xvelocity) * (score / 3)));
                            yvelocity = ((yvelocity / abs(yvelocity) * (score / 6)));
                        }
                    }
                }
            }
        }
    }else{
        xvelocity = (xvelocity / abs(xvelocity) * 2);
        yvelocity = (yvelocity / abs(yvelocity));
    }
    [ground[1] setPosition:ccp(144, playercenty)];
    [ground[2] setPosition:ccp(winSize.width - 144, playercenty)];
}
-(void)collisionDetection:(ccTime)delta{
    
    wallcollision(1);
    wallcollision(2);
    CGSize winSize = [CCDirector sharedDirector].winSize;
    
    if(playerleft < 0){//works
        xvelocity = abs(xvelocity);
        score = 0;
    }
    if(playerright > winSize.width){//works
        xvelocity = -abs(yvelocity);
    }
    if(playerbot < 0){//works
        yvelocity = abs(yvelocity);
    }
    if(playertop > winSize.height){//works
        yvelocity = -abs(xvelocity);
    }
}
void wallcollision(int alpha){
    if(playertop >= ground[alpha].position.y - ground[alpha].boundingBox.size.height/2){
        if(playerleft < ground[alpha].position.x + ground[alpha].boundingBox.size.width/2 && playerright > ground[alpha].position.x - ground[alpha].boundingBox.size.width/2){
            if(playertop < (ground[alpha].position.y - ground[alpha].boundingBox.size.height/2 + abs(yvelocity))){
                yvelocity = -abs(yvelocity);
            }
        }
    }
    if(playerright >= ground[alpha].position.x - ground[alpha].boundingBox.size.width/2){
        if(playertop > ground[alpha].position.y - ground[alpha].boundingBox.size.height/2 && playerbot < ground[alpha].position.y + ground[alpha].boundingBox.size.height/2){
            if(playerright < (ground[alpha].position.x - ground[alpha].boundingBox.size.width/2 + abs(xvelocity))){
                xvelocity  = -abs(xvelocity);
                score++;
            }
        }
    }
    if(playerleft <= ground[alpha].position.x + ground[alpha].boundingBox.size.width/2){
        if(playertop >= ground[alpha].position.y - ground[alpha].boundingBox.size.height/2 && playerbot <= ground[alpha].position.y + ground[alpha].boundingBox.size.height/2){
            if(playerleft > (ground[alpha].position.x + ground[alpha].boundingBox.size.width/2 - abs(xvelocity))){
                xvelocity = abs(xvelocity);
                score ++;
            }
        }
    }
    if(playerbot <= ground[alpha].position.y + ground[alpha].boundingBox.size.height/2){
        if(playerleft < ground[alpha].position.x + ground[alpha].boundingBox.size.width/2 && playerright > ground[alpha].position.x - ground[alpha].boundingBox.size.width/2){
            if(playerbot > (ground[alpha].position.y + ground[alpha].boundingBox.size.height/2 - abs(yvelocity))){
                yvelocity = abs(yvelocity);
            }
        }
    }
    
}

/*-(void) registerWithTouchDispatcher{
    [[[CCDirector sharedDirector]touchDispatcher]addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
    
    [ground[1] setPosition:ccp(144, touchLocation.y)];
    
    return true;
}
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
    
    [ground[1] setPosition:ccp(144, touchLocation.y)];
    
}*/
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

#pragma mark GameKit delegate


@end
